package com.liang.exam.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.exam.entity.Teacher;
import com.liang.exam.mapper.AdminMapper;
import com.liang.exam.service.AdminService;
import com.liang.exam.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void updateTeacher(int teacherId, String userName, String role) {
            adminMapper.updateTeacher(teacherId, userName, role);
    }

    @Override
    public void deleteTeacher(int teacherId) {
        adminMapper.deleteTeacher(teacherId);
    }

    @Override
    public void resetPassword(int teacherId) {
        adminMapper.resetPassword(teacherId);
    }

    @Override
    public void addTeacher(String account, String userName, String role) {
        String password = Md5Util.getMd5Encryption("123456");// 默认密码为：123456 (经过md5加密
        adminMapper.addTeacher(account, password,userName, role);
    }

    @Override
    public PageInfo<Teacher> findTeacher(Teacher teacher,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = adminMapper.findTeacher(teacher);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        return pageInfo;
    }

    @Override
    public PageInfo<Teacher> selectAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = adminMapper.selectAll();
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        return pageInfo;
    }

    @Override
    public void updatePassword(String newPwd, Integer id) {
        newPwd = Md5Util.getMd5Encryption(newPwd);
        adminMapper.updatePassword(newPwd, id);
    }

    @Override
    public Teacher findTeacherById(Integer id) {
        return adminMapper.findTeacherById(id);
    }


}
